package com.farm.configer.service.impl;

import com.farm.configer.domain.Configerform;
import com.farm.configer.form.ConfigerFormGenerator;
import com.farm.configer.form.FormModel;
import com.farm.core.time.TimeTool;
import org.apache.log4j.Logger;
import com.farm.configer.dao.ConfigerformDaoInter;
import com.farm.configer.service.ConfigerformServiceInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBRuleList;
import com.farm.core.sql.query.DBSort;
import com.farm.core.sql.query.DataQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：配置表单服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class ConfigerformServiceImpl implements ConfigerformServiceInter {
	@Resource
	private ConfigerformDaoInter configerformDaoImpl;

	private static final Logger log = Logger.getLogger(ConfigerformServiceImpl.class);

	@Override
	@Transactional
	public Configerform insertConfigerformEntity(Configerform entity, LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setEuser(user.getId());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		return configerformDaoImpl.insertEntity(entity);
	}

	@Override
	@Transactional
	public Configerform editConfigerformEntity(Configerform entity, LoginUser user) {
		Configerform entity2 = configerformDaoImpl.getEntity(entity.getId());
		entity2.setEuser(user.getId());
		entity2.setEtime(TimeTool.getTimeDate14());
		if (entity.getValid() != null) {
			entity2.setValid(entity.getValid());
		}
		if (entity.getModel() != null) {
			entity2.setModel(entity.getModel());
		}
		if (entity.getConfigerid() != null) {
			entity2.setConfigerid(entity.getConfigerid());
		}
		if (entity.getName() != null) {
			entity2.setName(entity.getName());
		}
		if (entity.getCode() != null) {
			entity2.setCode(entity.getCode());
		}
		if (entity.getSort() != null) {
			entity2.setSort(entity.getSort());
		}
		if (entity.getPcontent() != null) {
			entity2.setPcontent(entity.getPcontent());
		}
		if (entity.getEnums() != null) {
			entity2.setEnums(entity.getEnums());
		}
		if (entity.getInnercode() != null) {
			entity2.setInnercode(entity.getInnercode());
		}
		configerformDaoImpl.editEntity(entity2);
		return entity2;
	}

	@Override
	@Transactional
	public void deleteConfigerformEntity(String id, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		configerformDaoImpl.deleteEntity(configerformDaoImpl.getEntity(id));
	}

	@Override
	@Transactional
	public Configerform getConfigerformEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return configerformDaoImpl.getEntity(id);
	}

	@Override
	@Transactional
	public DataQuery createConfigerformSimpleQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query, "CBTG_CONFIGER_FORM",
				"ID,VALID,INNERCODE,MODEL,CONFIGERID,NAME,SORT,CODE,PCONTENT,PSTATE,CUSER,EUSER,CTIME,ETIME");
		dbQuery.addDefaultSort(new DBSort("SORT", "ASC"));
		return dbQuery;
	}

	@Override
	@Transactional
	public void initForms(String configerId, LoginUser currentUser) {
		FormModel dormModel = ConfigerFormGenerator.getConfigerModelInstance(configerId);
		List<Configerform> forms = dormModel.getForms();
		if (forms != null) {
			List<DBRule> rules = DBRuleList.getInstance().add(new DBRule("CONFIGERID", configerId, "=")).toList();
			configerformDaoImpl.deleteEntitys(rules);
			for (Configerform from : forms) {
				insertConfigerformEntity(from, currentUser);
			}
		}
	}
}
